# -*- coding: utf-8 -*-
# @Author : Cyril
# @Date   : 2019/10/18
# @File   : EG15_ShareholderCnt.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:股东数量
code:EG15_ShareholderCnt
tableName:
columnName:
groups:企业通用模块
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''



from datetime import datetime
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        if eg in null_type_list:
            return None
        if isinstance(eg, list):
            return eg
        else:
            return None
    except:
        return None


# 统一转成datetime对象，只能转年月日的字符串
def date_format(str):
    import re
    r = re.compile(r'\D+')
    try:
        time = str[0:10]
        date = datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date



def EG15_ShareholderCnt():

    try:
        shareHolderList = FormatData('shareHolderList')
        if shareHolderList is None:
            return u'缺失值'
        else:
            if len(shareHolderList) == 0:
                return 0
            else:
                result = set()
                flag = False
                for i in shareHolderList:
                    shareholderName = i.get('shareholderName')
                    if shareholderName not in null_type_list:
                        flag = True
                        result.add(shareholderName)
                return len(result) if flag else u'缺失值'
    except:
        return u'缺失值'

result = EG15_ShareholderCnt()

